import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.sbear.service.rpc.MyDubboService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 主程序
 *
 * @author xxyWi
 * @date 2021/01/27
 */
public class MainProgram {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        app.start();
        MyDubboService service = app.getBean(MyDubboService.class);

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 2000, 1, TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(3), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r);
            }
        });

        while (true){
            Thread.sleep(10);
            threadPoolExecutor.execute(()->{
                int i = new Random().nextInt(3);
                if (i == 0){
                    try {
                        service.methodA("呼呼呼");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (i == 1){
                    try {
                        service.methodB("HAHAHA");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (i == 2){
                    try {
                        service.methodC("DADADA");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("org.sbear.service.rpc")
    @EnableDubbo
    static class ConsumerConfiguration {
    }
}
